package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.dao.SysLogUseDao;
import com.cyt.sys.model.po.SysLogUse;
import com.cyt.sys.model.vo.SysLogUseVo;
import com.cyt.sys.service.SysLogUseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;

/**
 *@Author: YuShengQiang
 *@Date: 2018-11-16 11:52
 *@Description:操作日志service实现类
 */
@Service("baseLogUseService")
public class SysLogUseServiceImpl extends BaseServiceImpl<SysLogUse> implements SysLogUseService {
    @Resource
    private SysLogUseDao sysLogUseDao;
    @Autowired
    public void setBaseDao(SysLogUseDao sysLogUseDao) {
        this.baseDao = sysLogUseDao;
    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-11-23 14:17
     *@Description: 分页查询
     */
    public ReturnResult page(SysLogUseVo sysLogUseVo, int pageSize, int pageNum)throws UnknownHostException,SocketException {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" create_time desc");
//        Example example = new Example(SysLogUse.class);
//        Example.Criteria criteria = example.createCriteria();
//        if(StringUtil.isNotEmpty(sysLogUseVo.getModuleName())){
//            criteria.andLike("moduleName","%" + sysLogUseVo.getModuleName() + "%");
//        }
//        if(StringUtil.isNotEmpty(sysLogUseVo.getOperatorId())){
//            criteria.andEqualTo("operatorId", sysLogUseVo.getOperatorId());
//        }
//        if(StringUtil.isNotEmpty(sysLogUseVo.getOperatorType())){
//            criteria.andEqualTo("operatorType", sysLogUseVo.getOperatorType());
//        }
        List<SysLogUseVo> list= sysLogUseDao.listLeftLogUserAndUser(sysLogUseVo);
       // List<SysLogUse> list=baseLogUseDao.selectByExample(example);
        InetAddress ia = InetAddress.getLocalHost();
        String ip =ia.toString();
        String operMac= SysUserServiceImpl.getLocalMac();
        String[] ips =ip.split("/");
        for(SysLogUseVo baseLogUse:list ){
            if(baseLogUse.getOperIp().equals(ips[1])&&StringUtil.isEmpty(baseLogUse.getOperMac())){
                baseLogUse.setOperMac(operMac);
            }
        }
        PageInfo<SysLogUseVo> page=new PageInfo<SysLogUseVo>(list);
        return new ReturnResult(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     *@Author: YuShengQiang
     *@Date: 2018-11-17 8:54
     *@Description: 获取集合
     */
    public List<SysLogUse> selectList(SysLogUseVo sysLogUseVo){
        Example example = new Example(SysLogUse.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysLogUseVo !=null){
            if(StringUtil.isNotEmpty(sysLogUseVo.getOperIp())){
                criteria.andEqualTo("operatorType", sysLogUseVo.getOperatorType());
            }
            if(StringUtil.isNotEmpty(sysLogUseVo.getOperIp())){
                criteria.andEqualTo("moduleName", sysLogUseVo.getModuleName());
            }
            if(StringUtil.isNotEmpty(sysLogUseVo.getOperIp())){
                criteria.andEqualTo("operatorId", sysLogUseVo.getOperatorId());
            }
        }
        return  sysLogUseDao.selectByExample(example);
    }


}
